﻿using Devil;
using LitJson;
using System.Collections.Generic;
using System.IO;

namespace TableCore.Plugin
{

    public class ExportValue
    {
        public readonly string name;
        public readonly GTType type;
        public readonly string value;
        public readonly string comment;
        public ExportValue(string name, GTType type, string value, string comment)
        {
            this.name = name;
            this.type = type;
            this.value = value;
            this.comment = comment;
        }
    }

    // 数据导出
    public interface IExportData
    {
        int DataId { get; }
        string Comment { get; }
        ExportFileID ExportFile { get; }
        ExportValue[] ExportValues { get; }
    }

    public interface IAsPrimary
    {
        string AsPrimaryCol { get; }
    }

    public interface IBeforeGenFormatter
    {
        void BeforeFormat(int currentColumn, object[] rowData);
    }

    /// <summary>
    ///  优化数据
    /// </summary>
    public interface IOptimizationGenData
    {
        ParallelDispatcher.Handler OptimizeColumn(GTFile.Content content, int currentVolumn);
    }

    // 数据类型输出格式化
    public interface IGenFormatter
    {
        //void Init(Dictionary<string, string> args, string content);
        bool IsValid(string data);
        IExportData ExportData(GTFile.Content content, string data, string cellName, string comment);
        JsonData Format(GTFile.Content content, string data, string cellName, string comment);
    }

    //public interface IGenXmlInitializer
    //{
    //    void Init(XmlElement element);
    //}

    public interface IGenCmdInitializer
    {
        void Init(GTType type, Dictionary<string, string> args, string content);
    }

    //// 数据输出时格式化
    //public interface IDataModify
    //{
    //    void Init(XmlElement element);

    //    bool PrepareTable(GTStatus status, string catgory, string tableName);

    //    GenData[] FixOutputData(string catgory, GenData data, GenData previours, GenData next);
    //}

    [System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple = true)]
    public class FormatterAttribute : System.Attribute
    {
        public string Alias { get; set; }
        public FormatterAttribute(string alias)
        {
            Alias = alias;
        }
        public FormatterAttribute() { }
    }

    public struct ExportFileID : System.IEquatable<ExportFileID>
    {
        int _id;
        string _excel;
        string _sheet;
        public int Id { get { return _id; } }
        public string excelFile { get { return _excel; } }
        public string excelSheet { get { return _sheet; } }

        public ExportFileID(string excel, string sheet)
        {
            this._excel = Path.GetFullPath(excel);
            this._sheet = sheet;
            this._id = StringUtil.IgnoreCaseToHash(StringUtil.Concat(_excel, "#", sheet));
        }
        public override int GetHashCode()
        {
            return _id;
        }

        public bool Equals(ExportFileID other)
        {
            return _id == other._id;
        }

        public override bool Equals(object obj)
        {
            if (obj is ExportFileID)
                return _id == ((ExportFileID)obj)._id;
            return false;
        }

        public override string ToString()
        {
            return string.Format("{0}#{1}", _excel, _sheet);
        }

        public static bool operator == (ExportFileID a, ExportFileID b)
        {
            return a._id == b._id;
        }
        public static bool operator !=(ExportFileID a, ExportFileID b)
        {
            return a._id != b._id;
        }

    }
}
